import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { AnnouncementService } from './announcement.service';
import { CurrentUser } from 'src/user/user.decorator';
import { UserSys } from 'src/user/userSys.entity';
import { readFileSync, unlinkSync } from 'fs';
import { Announcement } from './announcement.entity';
import { NotFound } from 'src/util/http';
import { RoleEnum } from 'src/role/role.enum';
import { FindOptionsWhere } from 'typeorm';

@Controller('api/ann')
@ApiTags("公告")
@UseGuards(AuthGuard("jwt"))
export class AnnouncementController {
    constructor(
        private readonly annService: AnnouncementService
    ) { }

    @Get("getmine")
    @ApiOperation({ summary: "获取我发布的公告" })
    async getmine(
        @CurrentUser() user: UserSys
    ) {
        const where: FindOptionsWhere<Announcement> = {}
        if (user.role === RoleEnum.director) where.system = { id: user.system.id }
        else if (user.role !== RoleEnum.boos) where.author = { id: user.id }
        return await this.annService.findBy(where)
    }

    @Get(":id/read")
    @ApiOperation({ summary: "查看公告" })
    async read(
        @Param("id") id: number,
        @CurrentUser() user: UserSys
    ) {
        if (!await this.annService.annread.exist({
            where: {
                user: {
                    id: user.id
                },
                ann: {
                    id
                }
            }
        })) {
            const entity = this.annService.annread.create({
                user,
                ann: await this.annService.ann.findOne({ where: { id } })
            });
            await this.annService.annread.save(entity);
        }
    }

    @Get(":id/content")
    @ApiOperation({ summary: "获取公告内容" })
    async content(
        @Param("id") id: number,
        @CurrentUser() user: UserSys
    ) {
        if (!await this.annService.annread.exist({
            where: {
                user: { id: user.id },
                ann: { id }
            },
        })) {
            const ann = await this.annService.ann.findOne({ where: { id } });
            if (!!ann) {
                const entity = this.annService.annread.create({
                    user,
                    ann
                })
                await this.annService.annread.save(entity)
            }
        }
        return readFileSync(Announcement.getPath(id)).toString()
    }

    @Post("publish")
    @ApiOperation({ summary: "发布公告" })
    async publish(
        @Body() body,
        @CurrentUser() user: UserSys
    ) {
        return await this.annService.save(
            this.annService.ann.create({
                author: user,
                system: user.system
            }),
            body
        )
    }

    @Post(":id/update")
    @ApiOperation({ summary: "修改公告" })
    async update(
        @Body() body,
        @CurrentUser() user: UserSys,
        @Param("id") id: number
    ) {
        return await this.annService.save(
            await this.annService.ann.findOne({
                where: {
                    id,
                    system: {
                        id: user.system.id
                    }
                }
            }),
            body
        )
    }

    @Get(":id/del")
    @ApiOperation({ summary: "删除公告" })
    async del(
        @Param("id") id: number
    ) {
        const ann = await this.annService.ann.findOne({ where: { id } });
        if (!ann) throw NotFound;
        await this.annService.annread.delete({
            ann: {
                id: ann.id
            }
        })
        const result = await this.annService.ann.delete({ id: ann.id })
        unlinkSync(Announcement.getPath(ann.id));
        return result;
    }

}
